#!/bin/bash
#
# Copyright (C) 1994-2021 Altair Engineering, Inc.
# For more information, contact Altair at www.altair.com.
#
# This file is part of both the OpenPBS software ("OpenPBS")
# and the PBS Professional ("PBS Pro") software.
#
# Open Source License Information:
#
# OpenPBS is free software. You can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# OpenPBS is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Commercial License Information:
#
# PBS Pro is commercially licensed software that shares a common core with
# the OpenPBS software.  For a copy of the commercial license terms and
# conditions, go to: (http://www.pbspro.com/agreement.html) or contact the
# Altair Legal Department.
#
# Altair's dual-license business model allows companies, individuals, and
# organizations to create proprietary derivative works of OpenPBS and
# distribute them - whether embedded or bundled with other software -
# under a commercial license agreement.
#
# Use of Altair's trademarks, including but not limited to "PBS™",
# "OpenPBS®", "PBS Professional®", and "PBS Pro™" and Altair's logos is
# subject to Altair's trademark licensing policies.

#
pbs_version="$2"
pbs_exec="$3"
pbs_home="${4:-/var/spool/pbs}"
have_systemd="${5:-0}"

if [ `basename $pbs_exec` = $pbs_version ]; then
    top_level=`dirname $pbs_exec`
    if [ -h $top_level/default ]; then
        link_target=`readlink $top_level/default`
        [ `basename "$link_target"` = $pbs_version ] && rm -f $top_level/default
    fi
fi
rm -f /opt/modulefiles/pbs/$pbs_version

case "$1" in
server|execution)
    [ -x /etc/init.d/pbs ] && /etc/init.d/pbs stop
    [ -x /sbin/chkconfig ] && /sbin/chkconfig --del pbs >/dev/null 2>&1
    rm -f /etc/rc.d/rc?.d/[KS]??pbs
    rm -f /var/tmp/pbs_boot_check
    if [ $have_systemd = 1 ]; then
        echo "have systemd"
        systemctl disable pbs
        rm -f /usr/lib/systemd/system-preset/95-pbs.preset
        if command -v python &> /dev/null && [ -f ${pbs_home}/mom_priv/hooks/pbs_cgroups.CF ]; then
            service_name=`cat ${pbs_home}/mom_priv/hooks/pbs_cgroups.CF | python -c "import sys, json; print(json.load(sys.stdin)['cgroup_prefix'])"`
            systemctl stop ${service_name}.service
            [ -d /sys/fs/cgroup/cpuset/${service_name}.service/jobid ] && rmdir /sys/fs/cgroup/cpuset/${service_name}.service/jobid
            [ -d /sys/fs/cgroup/cpuset/${service_name}.service ] && rmdir /sys/fs/cgroup/cpuset/${service_name}.service
        fi
        [ -d /sys/fs/cgroup/cpuset/pbspro.service/jobid ] && rmdir /sys/fs/cgroup/cpuset/pbspro.service/jobid
        [ -d /sys/fs/cgroup/cpuset/pbspro.service ] && rmdir /sys/fs/cgroup/cpuset/pbspro.service
        exit 0
    fi
    ;;
esac
